﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElectroSystem.Models;
using Common;
using Common.Views;

namespace ElectroSystem.Controllers
{
    public class ProductsController : MenusController
    {
        //
        // GET: /Products/
        [Authorize]
        public ActionResult Index()
        {
           List<Product> products = new ProductsServ.ProductsServiceClient().GetAllProducts().ToList();
           return View("Index", products);
        }

        public PartialViewResult GetCommentsByProductID(int productID)
        {
            List<ProductComment> comments = new ProductsServ.ProductsServiceClient().GetCommentsByProductID(productID).ToList();
            return PartialView("_comments", comments);
        }

        public string AddToCart(int pId, int quantity)
        {
            string value = new ProductsServ.ProductsServiceClient().AddToCart(HttpContext.User.Identity.Name, pId, quantity);
            return value;
        }

        [HttpPost]
        public ActionResult ViewCart(ProductsModel pm)
        {
            List<CartView> products;
            try
            {
                if ((products = new ProductsServ.ProductsServiceClient().GetProductsFromCart(HttpContext.User.Identity.Name).ToList()) != null)
                {
                    pm.OrderID = Guid.NewGuid();
                    new OrdersServ.OrdersServiceClient().AddOrder(HttpContext.User.Identity.Name, pm.OrderID, products);
                    List<Product> allproducts = new ProductsServ.ProductsServiceClient().GetAllProducts().ToList();
                    return View("Index", allproducts);
                }
                else
                {
                    ViewBag.Message = "No products found";
                    return View();
                }
                
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                return View();
            }
        }

        
        public ActionResult ViewCart()
        {
            List<CartView> products = new ProductsServ.ProductsServiceClient().GetProductsFromCart(HttpContext.User.Identity.Name).ToList();
            
            decimal totalPrice = 0;

            foreach (CartView p in products)
            {
                totalPrice += (p.Price * p.Quantity);
            }

            ViewBag.TotalPrice = totalPrice;
            return View("ViewCart", products);
        }

        public PartialViewResult ReturnAverageRating(int productID)
        {
            List<int> ratings = new ProductsServ.ProductsServiceClient().ReturnAverageRating(productID).ToList();
            int total = 0;
            foreach (int i in ratings)
            {
                total += i;
            }
            double ratingsNumber = ratings.Count();
            double average = Math.Round((total / ratingsNumber), 2);
            
            
            ViewData["averageRating"] = average;
            ViewData["productID"] = productID;
            
            return PartialView("_avgrating", average);
        }

        
        
    }
} 
